package com.systop.asset.service.impl;

import com.systop.asset.entity.Inventory;
import com.systop.asset.entity.MaterialTransfer;
import com.systop.asset.entity.MaterialTransferDetail;
import com.systop.asset.entity.vo.MaterialTransferDetailVo;
import com.systop.asset.entity.vo.MaterialTransferVo;
import com.systop.asset.exception.SystemException;
import com.systop.asset.mapper.InventoryMapper;
import com.systop.asset.mapper.MaterialTransferMapper;
import com.systop.asset.service.IMaterialTransferService;
import com.systop.asset.utils.RandomCodeGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MaterialTransferServiceImpl implements IMaterialTransferService {


    @Autowired
    private MaterialTransferMapper materialTransferMapper;
    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public MaterialTransferVo getMaterialTransferList(int page, int pageSize, String orderNo) {
        page = (page - 1) * pageSize;
        return materialTransferMapper.selectTransferList(page, pageSize, orderNo);
    }

    @Override
    public MaterialTransferDetailVo getMaterialTransferDeatilList(int id) {
        return materialTransferMapper.selectTransferDetail(id);
    }


    @Override
    @Transactional
    public Long addMaterialTransfer(MaterialTransferVo materialTransferVo) {
        MaterialTransfer materialTransfer = new MaterialTransfer();
        BeanUtils.copyProperties(materialTransferVo, materialTransfer);
        materialTransfer.setOrderNo(RandomCodeGenerator.generateTransferCode());
        materialTransfer.setStatus(0L);
        materialTransfer.setOperatorId((Long) SecurityContextHolder.getContext().getAuthentication().getCredentials());
        materialTransfer.setOperator((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        materialTransfer.setCreateTime(new Date());

        System.out.println(materialTransfer);
        int mtRows = materialTransferMapper.insertMaterialTransfer(materialTransfer);
        if (mtRows == 0) {
            throw new SystemException(500, "耗材调拨添加失败");
        }
        for (MaterialTransferDetail item : materialTransferVo.getMaterialTransferDetailList()) {
            item.setTransferId(materialTransfer.getId());
            item.setMaterialId(item.getMaterialId());
            item.setMaterialCode(item.getMaterialCode());
            item.setMaterialName(item.getMaterialName());
            item.setSerialUnit(item.getSerialUnit());
            item.setMaterialCategory(item.getMaterialCategory());
            item.setMaterialSpecification(item.getMaterialSpecification());
            item.setTransferCount(item.getTransferCount());
            int mtdRows = materialTransferMapper.insertMaterialTransferDetail(item);
            System.out.println("----------->" + mtdRows);
            if (mtdRows == 0) {
                throw new SystemException(500, "耗材调拨详情添加失败");
            }
            /*
             *出库查询数据列表 更新出库的数据*/
            Inventory inventory = new Inventory();
            inventory.setMaterialId(item.getMaterialId());
            inventory.setWarehouseId(materialTransfer.getFromWarehouseId());
            List<Inventory> fromWarehouseData = inventoryMapper.selectInventoryList(inventory);
            if (fromWarehouseData.isEmpty()) {
                throw new SystemException(500, "耗材原始仓库错误");
            }
            int fResult = materialTransferMapper.updateMaterialInventorySub(materialTransfer.getFromWarehouseId(), item.getMaterialId(), item.getTransferCount());
            if (fResult == 0) {
                throw new SystemException(500, "更新出库存失败");
            }

            /*
             *
             * 入库的查询数据列表，有数据更新入库，没有插入*/
            inventory.setWarehouseId(materialTransfer.getToWarehouseId());
            inventory.setMaterialId(item.getMaterialId());
            List<Inventory> toWarehouseData = inventoryMapper.selectInventoryList(inventory);
            if (!toWarehouseData.isEmpty()) {
                int toResult = materialTransferMapper.updateMaterialInventoryAdd(materialTransfer.getToWarehouseId(), item.getMaterialId(), item.getTransferCount());
                if (toResult == 0) {
                    throw new SystemException(500, "更新入库存失败");
                }
            } else {
                inventory.setMaterialId(item.getMaterialId());
                inventory.setWarehouseId(materialTransfer.getToWarehouseId());
                inventory.setQuantity((long) item.getTransferCount());
                inventory.setCategoryId(item.getCategoryId());
                inventoryMapper.insertInventory(inventory);
            }
        }
        return materialTransfer.getId();
    }


    @Override
    @Transactional
    public int upMTStatus(int mtId, int status) {
        if (status == 3) {
            MaterialTransferDetailVo materialTransferDetailVo = materialTransferMapper.selectTransferDetail(mtId);
            System.out.println(materialTransferDetailVo);
            for (MaterialTransferDetail item : materialTransferDetailVo.getMtdList()) {
                materialTransferMapper.updateMaterialInventoryAdd(materialTransferDetailVo.getFromWarehouseId(), item.getMaterialId(), item.getTransferCount());
                materialTransferMapper.updateMaterialInventorySub(materialTransferDetailVo.getToWarehouseId(), item.getMaterialId(), item.getTransferCount());
            }
            materialTransferMapper.updateMaterialTransferStatus(mtId, status);
        } else {
            materialTransferMapper.updateMaterialTransferStatus(mtId, status);
        }
        return 0;
    }


}
